/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.mybatis.service;

import org.tianyun.cloud.common.domain.BaseDomain;
import org.tianyun.cloud.mybatis.mapper.BaseMapper;

import java.io.Serializable;
import java.util.List;

/**
 * BaseService
 *
 * @author ebert_chan
 */
public interface BaseService<PK extends Serializable, T extends BaseDomain> {

    /**
     * 抽象方法，用于获取对应Mapper
     *
     * @return BaseMapper<PK, T> Mapper
     * @author ebert_chan
     */
    <M extends BaseMapper<PK, T>> M getBaseMapper();

    /**
     * 保存
     *
     * @param domain 数据库表映射对象
     * @return int 主键pk
     * @author ebert_chan
     */
    default Integer save(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.save(domain);
    }

    /**
     * 批量保存
     *
     * @param domainList 数据库表映射对象
     * @return Integer 影响行数
     * @auther ebert_chan
     */
    default Integer batchSave(List<T> domainList) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.batchSave(domainList);
    }

    /**
     * 逻辑删除
     *
     * @param pk 主键
     * @return int 删除行数
     * @author ebert_chan
     */
    default int remove(PK pk) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.remove(pk);
    }

    /**
     * 根据条件删除
     *
     * @param domain 数据库表映射对象
     * @return Integer 删除行数
     * @auther ebert_chan
     */
    default Integer removeBySelective(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.removeBySelective(domain);
    }

    /**
     * 批量删除
     *
     * @param pkList 主键列表
     * @return Integer 删除行数
     * @auther ebert_chan
     */
    default Integer batchRemove(List<PK> pkList) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.batchRemove(pkList);
    }

    /**
     * 更新
     *
     * @param domain 数据库表映射对象
     * @return int 更新行数
     * @author ebert_chan
     */
    default int update(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.update(domain);
    }

    /**
     * 选择性更新
     *
     * @param domain 数据库表映射对象
     * @return Integer 更新行数
     * @auther ebert_chan
     */
    default Integer updateBySelective(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.updateBySelective(domain);
    }

    /**
     * 查询
     *
     * @param pk 主键
     * @return T 实体类
     * @author ebert_chan
     */
    default T get(PK pk) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.get(pk);
    }

    /**
     * 根据指定条件查询数据
     *
     * @param domain
     * @return T 数据表映射对象
     * @auther ebert_chan
     */
    default T getBySelective(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.getBySelective(domain);
    }

    /**
     * 根据查询条件筛选数据，结果以List集合返回
     *
     * @param domain 数据库表映射对象
     * @return List<T> 数据库表映射对象集合
     * @author ebert_chan
     */
    default List<T> listBySelective(T domain) {
        BaseMapper<PK, T> baseMapper = this.getBaseMapper();
        return baseMapper.listBySelective(domain);
    }

}
